package org.example.myleet.p1582;

public class Solution {
    public int numSpecial(int[][] mat) {
        int m = mat.length, n = mat[0].length;
        boolean[] possibleRow = new boolean[m];
        for (int i = 0; i < m; ++i) {
            possibleRow[i] = true;
        }
        for (int i = 0; i < m; ++i) {
            if (!possibleRow[i]) {
                continue;
            }
            int rowSum = 0, jid = 0;
            for (int j = 0; j < n; ++j) {
                if (mat[i][j] == 1) {
                    jid = j;
                }
                rowSum += mat[i][j];
            }
            if (rowSum != 1) {
                possibleRow[i] = false;
                continue;
            }
            boolean repeatOccur = false;
            for (int i1 = 0; i1 < m; ++i1) {
                if (i1 == i) continue;;
                if (mat[i1][jid] == 1) {
                    possibleRow[i1] = false;
                    repeatOccur = true;
                }
            }
            if (repeatOccur) {
                //如果本列jid出现不止一个1，那么第i行也无缘进入答案
                possibleRow[i] = false;
            }
        }
        int res = 0;
        for (boolean p : possibleRow) {
            if (p) {
                ++res;
            }
        }
        return res;
    }
}
